package cn.cxq.learning.double_linked_list;

/**
 * 双向链表
 */
public class DoubleLinkedList {

    private Node head;

    public DoubleLinkedList() {
        this.head = new Node(0);
    }

    /**
     * 直接在末尾添加
     * @param node
     */
    public void add(Node node){
        Node temp = this.head;
        while (temp.nest != null){
            temp = temp.nest;
        }
        temp.nest = node;
        node.pre = temp;
        System.out.println("插入成功~~~");
    }

    /**
     * 按编号添加，如果有重复的就不给添加
     * @param node
     */
    public void addByOrder(Node node){
        Node temp = this.head;
        while (temp.nest != null){
            if (temp.nest.no > node.no) break;
            if (temp.nest.no == node.no) {
                System.out.println("编号已存在，无法插入节点~~~");
                return;
            }
            temp = temp.nest;
        }
        node.nest = temp.nest;
        if(temp.nest != null) temp.nest.pre = node;
        temp.nest = node;
        node.pre = temp;
        System.out.println("插入成功~~~");
    }

    public void delete(int no){
        Node temp = this.head;
        if (temp.nest == null) {
            System.out.println("链表为空~~~");
            return;
        }
        while (temp != null){
            temp = temp.nest;
            if (temp.no == no) break;
        }
        if (temp!=null){
            temp.pre.nest = temp.nest;
            if (temp.nest!=null){
                temp.nest.pre = temp.pre;
            }
            System.out.println("删除成功~~~");
        }else System.out.println("删除失败，未找到节点~~~");
    }

    public void update(Node node){
        Node temp = this.head;
        if (temp.nest == null) {
            System.out.println("链表为空~~~");
            return;
        }
        while (temp.nest != null){
            if (temp.nest.no == node.no) break;
            temp = temp.nest;
        }
        if (temp.nest!=null){
            node.nest = temp.nest.nest;
            if (temp.nest.nest != null) node.nest.pre = node;
            temp.nest = node;
            node.pre = temp;
            System.out.println("修改成功~~~");
        }else System.out.println("修改失败，未找到节点~~~");
    }

    public void list(){
        Node temp = this.head;
        if (temp.nest == null) {
            System.out.println("链表为空~~~");
            return;
        }
        while (temp.nest != null){
            temp = temp.nest;
            System.out.println(temp);
        }
    }

    //反向打印双向链表
    public void reverseList(){
        Node temp = this.head;
        if (temp.nest == null) {
            System.out.println("链表为空~~~");
            return;
        }
        while (temp.nest != null){
            temp = temp.nest;
        }
        while (temp.pre != null){
            System.out.println(temp);
            temp = temp.pre;
        }
    }
}

class Node{

    int no;
    String msg;
    Node nest;
    Node pre; //上一节点

    public Node(int no, String msg) {
        this.no = no;
        this.msg = msg;
        this.nest = null;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", msg='" + msg +
                '}';
    }

    public Node(int no){
        this.no =no;
        this.msg="";
        this.nest = null;
        this.pre = null;
    }
}
